package com.unohacha.qqj.fzdr.my;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.utils.TemporaryDataUtil;
import com.unohacha.qqj.fzdr.personal.bean.UserInfoBean;
import com.unohacha.qqj.fzdr.personal.presenter.IPersonalPresenter;
import com.unohacha.qqj.fzdr.personal.presenter.PersonalPresenter;
import com.unohacha.qqj.fzdr.personal.view.IPersonal;
import com.unohacha.qqj.fzdr.personal.view.activity.ConfirmPhoneActivity;
import com.unohacha.qqj.fzdr.personal.view.activity.EditInfoActivity;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class MyInfoCenterActivity extends BaseActivity implements IPersonal {

    // 1-企业名称；2-企业地址；3-公司电话；4-公司简介；5-用户名
    /* start 标题栏 start */
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    /* end 标题栏 end */

    @BindView(R.id.person_image)
    RoundedImageView personImage;
    @BindView(R.id.my_collection)
    LinearLayout myCollection;
    @BindView(R.id.person_username)
    TextView personUsername;
    @BindView(R.id.person_phone)
    TextView personPhone;
    @BindView(R.id.person_weixin)
    TextView personWeixin;
    @BindView(R.id.enter_name)
    TextView enterName;
    @BindView(R.id.enter_address)
    TextView enterAddress;
    @BindView(R.id.enter_phone)
    TextView enterPhone;
    @BindView(R.id.enter_introduce)
    TextView enterIntroduce;

    IPersonalPresenter personalPresenter;

    @OnClick(R.id.my_collection)
    void onMyCollectionClick() {
        Toast.makeText(getApplicationContext(), "修改头像功能未完成", Toast.LENGTH_SHORT).show();
    }

    // 修改用户名
    @OnClick(R.id.ll_username)
    void onUserNameClick() {
        Intent intent = new Intent(this, EditInfoActivity.class);
        intent.putExtra("typeid", "5");
        startActivity(intent);
    }

    // 修改企业名称
    @OnClick(R.id.ll_enterprise_name)
    void onEnterpriseNameClick() {
        Intent intent = new Intent(this, EditInfoActivity.class);
        intent.putExtra("typeid", "1");
        startActivity(intent);
    }

    // 修改企业地址
    @OnClick(R.id.ll_enterprise_address)
    void onEnterpriseAddressClick() {
        Intent intent = new Intent(this, EditInfoActivity.class);
        intent.putExtra("typeid", "2");
        startActivity(intent);
    }

    // 修改公司电话
    @OnClick(R.id.ll_enterprise_mobile)
    void onEnterpriseMobileClick() {
        Intent intent = new Intent(this, EditInfoActivity.class);
        intent.putExtra("typeid", "3");
        startActivity(intent);
    }

    // 修改企业简介
    @OnClick(R.id.enter_introduce)
    void onEnterpriseIntroductionClick() {
        Intent intent = new Intent(this, EditInfoActivity.class);
        intent.putExtra("typeid", "4");
        startActivity(intent);
    }

    @OnClick(R.id.ll_mobile)
    void onMobileChangeClick() {
        jumpToOtherActivity(this, ConfirmPhoneActivity.class);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_info_center;
    }

    @Override
    protected void onResume() {
        super.onResume();
        personalPresenter.getUserInfo(Prefer.getInstance().getToken());
        showLoading("加载中...");
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setText("个人信息");
        commonRightImage.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {
//        super.initData();
        // 初始化用户数据
        personalPresenter = new PersonalPresenter(this);
        // 请求用户信息数据
        if (TemporaryDataUtil.getUserInfoBean() != null) {
            showUserInfo(TemporaryDataUtil.getUserInfoBean());
        } else {
            personalPresenter.getUserInfo(Prefer.getInstance().getToken());
        }
        showLoading("加载中...");
    }

    @OnClick(R.id.common_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showUserInfo(UserInfoBean userInfoBean) {
        // 显示 用户信息 每一个都要判断 可能会有 空值出现
        if (userInfoBean.getThumb() != null && !userInfoBean.getThumb().toString().equals("")) { // 头像
            Glide.with(this).load(userInfoBean.getThumb()).into(personImage);
        } else {
            Glide.with(this).load(R.drawable.ic_default_photo).into(personImage);
        }
        if (!userInfoBean.getMember_id().equals("")) { // 用户名 没有参数
            personUsername.setText(userInfoBean.getMember_id());
        } else {
            personUsername.setText("");
        }
        personPhone.setText(userInfoBean.getTelephone());// 手机号

        if (!userInfoBean.getMember_id().equals("")) { // 微信号
            personWeixin.setText(userInfoBean.getMember_id());
        } else {
            personWeixin.setText("未绑定");
        }
        if (userInfoBean.getCompany_name() != null) { // 企业名称
            enterName.setText(userInfoBean.getCompany_name().toString());
        } else {
            enterName.setText("");
        }
        if (userInfoBean.getCompany_address() != null) { // 企业地址
            enterAddress.setText(userInfoBean.getCompany_address().toString());
        } else {
            enterAddress.setText("");
        }
        if (userInfoBean.getCompany_tel() != null) { // 企业电话
            enterPhone.setText(userInfoBean.getCompany_tel().toString());
        } else {
            enterPhone.setText("");
        }
        if (userInfoBean.getCompany_desc() != null) { // 企业简介
            enterIntroduce.setText(userInfoBean.getCompany_desc().toString());
        } else {
            enterIntroduce.setText("");
        }
        hideLoading();
    }
}
